﻿using BBICMS.Model;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Web;

namespace BBICMS.Repository
{
   public static class LanguageHelper
    {
       
        public static IEnumerable<Language> AvailableLanguages() {
            ApplicationUnit _unit = new ApplicationUnit();
            return _unit.Languages.All();
        }


        public static IEnumerable<Language> ActiveLanguages()
        {           
            return AvailableLanguages().Where(l=>l.Active).OrderByDescending(l=>l.IsDefault);
        }

        /// <summary>
        /// Returns true if the language is a right-to-left language. Otherwise, false.
        /// </summary>
        public static bool IsRighToLeft()
        {
            return Thread.CurrentThread.CurrentCulture.TextInfo.IsRightToLeft;

        }

        public static bool IsLanguageAvailable(string lang)
        {
            return AvailableLanguages().Where(a => a.CultureName.Equals(lang)).FirstOrDefault() != null ? true : false;
        }
        public static string GetDefaultCulture()
        {
            return AvailableLanguages().OrderByDescending(a=>a.IsDefault).FirstOrDefault().CultureName;
        }


        /// <summary>
        /// Returns a valid culture name based on "name" parameter. If "name" is not valid, it returns the default culture "en-US"
        /// </summary>
        /// <param name="name" />Culture's name (e.g. en-US)</param>
        public static string GetImplementedCulture(string name)
        {
            // make sure it's not null
            if (string.IsNullOrEmpty(name))
                return GetDefaultCulture(); // return Default culture
                                            // make sure it is a valid culture first
            if (AvailableLanguages().Where(c => c.CultureName.Equals(name, StringComparison.InvariantCultureIgnoreCase)).Count() == 0)
                return GetDefaultCulture(); // return Default culture if it is invalid
                                            // if it is implemented, accept it
            
                             // Find a close match. For example, if you have "en-US" defined and the user requests "en-GB", 
                             // the function will return closes match that is "en-US" because at least the language is the same (ie English)  
            var n = GetNeutralCulture(name);
            foreach (var c in AvailableLanguages())
                if (c.CultureName.StartsWith(n))
                    return c.CultureName;
            // else 
            // It is not implemented
            return GetDefaultCulture(); // return Default culture as no match found
        }

        public static string GetNeutralCulture(string name)
        {
            if (!name.Contains("-")) return name;
            return name.Split('-')[0]; // Read first part only. E.g. "en", "es"
        }

        public static string GetCurrentCulture()
        {
            return Thread.CurrentThread.CurrentCulture.Name;
        }
        public static string GetCurrentNeutralCulture()
        {
            return GetNeutralCulture(Thread.CurrentThread.CurrentCulture.Name);
        }


        //public void SetLanguage(string lang)
        //{
        //    try
        //    {
        //        if (!IsLanguageAvailable(lang)) lang = GetDefaultLanguage();
        //        var cultureInfo = new CultureInfo(lang);
        //        Thread.CurrentThread.CurrentUICulture = cultureInfo;
        //        Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(cultureInfo.Name);
        //        HttpCookie langCookie = new HttpCookie("culture", lang);
        //        langCookie.Expires = DateTime.Now.AddYears(1);
        //        HttpContext.Current.Response.Cookies.Add(langCookie);
        //    }
        //    catch (Exception) { }
        //}
    }

}

